from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from Units import uConst
from Units.templates.uIndexRender import IndexRender
from Units.uUserFlow import UserFlow
import os



class MainPage(webapp.RequestHandler):
    
    def get(self):
        self.path = os.path.dirname(__file__)
        
        
        renderer = IndexRender(self)
        renderer.Render()
        

class Inicio(webapp.RequestHandler):
    def get(self):
        self.redirect(uConst.LOGIN)  #No need for render

class LogOut(webapp.RequestHandler):
    
    def get(self):
        user = UserFlow(self)
        if user.isLogged():
            user.LogOut()
        else:
            user.LogIn()

class Nueva_Tarea(webapp.RequestHandler):
    def post(self):
        
        user = UserFlow(self)
        if user.isLogged():

            user.crearTarea(self.request.get('task_Asunto'),
                                      self.request.get('task_categoria'),
                                       self.request.get('task_Prioridad'),
                                       self.request.get('task_notas'),
                                       self.request.get('task_Vencimiento'),
                                       self.request.get('task_TiempoEst'))
            
            self.redirect('/')
        else:
            user.LogIn()

class Modificar_Tarea(webapp.RequestHandler):
    
    def post(self):
        user = UserFlow(self)
        if user.isLogged():
            
            user.modificarTarea(self.request.get('tarea_aModificar'),
                                self.request.get('task_Asunto2'),
                                self.request.get('task_categoria2'),
                                self.request.get('task_Prioridad2'),
                                self.request.get('task_notas2'),
                                self.request.get('task_Vencimiento2'),
                                self.request.get('task_TiempoEst2'))
            
            self.redirect('/')
        else:
            user.LogIn()
            
class Cambiar_Estado(webapp.RequestHandler):
    
    def post(self): #Cambia estado de Pendiente a Completada y viceversa
        user = UserFlow(self)
        if user.isLogged():
            user.getTareaPorClave(self.request.get('tarea_aCambiarEstado')).CambiarEstado()
            self.redirect('/')
        else:
            user.LogIn()

class Posponer_Tarea(webapp.RequestHandler):
    def post(self):
        user = UserFlow(self)
        if user.isLogged():
            tarea = user.getTareaPorClave(self.request.get('tarea_aPosponer'))
            if (tarea.getVencimiento <> "No especificado"):
                tarea.Posponer()
                self.redirect('/')
            else:
                pass
        else:
            user.LogIn()

class Compartir_Tarea(webapp.RequestHandler):
    
    def post(self):
        user = UserFlow(self)
        if user.isLogged():
            aTarea = user.getTareaPorClave(self.request.get('tarea_aCompartir'))
            aTarea.Compartir(self.request.get('user_email'))
            self.redirect('/')
        else:
            user.LogIn()

class Eliminar_Tarea(webapp.RequestHandler):
    
    def post(self):
        user = UserFlow(self)
        if user.isLogged():
            aTarea = user.getTareaPorClave(self.request.get('tarea_aEliminar'))
            aTarea.EliminarTarea()
            self.redirect('/')
        else:
            user.LogIn()
            
class Cambiar_Tema(webapp.RequestHandler):
    
    def post(self):  #Multiples Hojas de Estilo Posibles
        user=UserFlow(self)
        if user.isLogged():
            user.getConf().setTheme(self.request.get('theme'))
            self.redirect('/')
        else:
            user.LogIn()

class Print(webapp.RequestHandler):
    
    def post(self):  #Imprime la tarea seleccionada unicamente, (Boton Imprimier del panel izquierdo)
        user=UserFlow(self)
        if user.isLogged(): 
            if self.request.get("tarea_aImprimir"):
                aImprimir = user.getTareaPorClave(self.request.get('tarea_aImprimir')).ImprimirTarea()
            values = {'lista_tareas': aImprimir}    
            path = os.path.join(os.path.dirname(__file__), uConst.PRINT)
            self.response.out.write(template.render(path,values))
        else:
            user.LogIn()
    
    def get(self): # Imprimie la Lista de todas las tareas pendientes, (boton Imprimir del panel superior derecho)
        user=UserFlow(self)
        if user.isLogged(): 
            aImprimir = user.getTasks()
            values = {'lista_tareas': aImprimir}    
            path = os.path.join(os.path.dirname(__file__), uConst.PRINT)
            self.response.out.write(template.render(path,values))
        else:
            user.LogIn()
    
    
application = webapp.WSGIApplication([
                                      ('/', MainPage),
                                      ('/inicio', Inicio),
                                      ('/logout',LogOut),
                                      ('/nueva_tarea', Nueva_Tarea),
                                      ('/modificar_tarea', Modificar_Tarea),
                                      ('/cambiar_estado', Cambiar_Estado),
                                      ('/posponer_tarea', Posponer_Tarea),
                                      ('/compartir_tarea', Compartir_Tarea),
                                      ('/eliminar_tarea', Eliminar_Tarea),
                                      ('/cambiar_tema', Cambiar_Tema),
                                      ('/print', Print)
                                      ], debug=True)


def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()